package com.dingreading.cloud.admin.controller;

import com.dingreading.cloud.admin.entity.GoodsSuppliers;
import com.dingreading.cloud.admin.service.GoodsSuppliersService;
import com.dingreading.cloud.admin.service.WhPurchaseGoodsService;
import com.dingreading.cloud.common.base.R;
import com.dingreading.cloud.common.util.PageBeanDto;
import com.dingreading.cloud.common.util.PageUtil;
import com.mybatisflex.core.paginate.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/system/goodsSuppliers")
@Api(tags = "总部-商品供应商的商品管理")
public class GoodsSuppliersController {

    @Resource
    protected GoodsSuppliersService goodsSuppliersService;
    @Resource
    protected WhPurchaseGoodsService whPurchaseGoodsService;


    @ApiOperation(value = "获取供应商的商品列表")
    @PostMapping(value = "/getGoodsSuppliers", produces = "application/json; charset=UTF-8")
    public R<Object> getGoodsSuppliers(
            @ApiParam(value = "分页公共类") PageUtil pageUtil,
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "供应商id") @RequestParam(value = "suppliersId", required = false) Long suppliersId,
            @ApiParam(value = "状态 1.启用 0.禁用") @RequestParam(value = "enabled", required = false) Integer enabled
    ) {
        Page<GoodsSuppliers> page = this.goodsSuppliersService.pageList(pageUtil, suppliersId, enabled);

        PageBeanDto data = new PageBeanDto();
        data.setTotal(page.getTotalRow());
        data.setList(page.getRecords());
        return R.ok(data);
    }


    @ApiOperation(value = "商品加入到供应商")
    @PostMapping(value = "/addGoods", produces = "application/json; charset=UTF-8")
    public R<Object> addGoods(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "供应商id") @RequestParam(value = "suppliersId") Long suppliersId,
            @ApiParam(value = "商品id") @RequestParam(value = "goodsId") Long goodsId,
            @ApiParam(value = "商品规格id") @RequestParam(value = "goodsTypeId") Long goodsTypeId,
            @ApiParam(value = "供应商价格") @RequestParam(value = "price", required = false) Double price
    ) {
        long l = goodsSuppliersService.getDuplicate(suppliersId, goodsId, goodsTypeId);
        if (l > 0) return R.fail("商品不能重复加入");

        GoodsSuppliers data = new GoodsSuppliers();
        data.setSupplierId(suppliersId);
        data.setGoodsId(goodsId);
        data.setGoodsTypeId(goodsTypeId);
        data.setPrice(price);
        data.setEnabled(1);
        goodsSuppliersService.saveOrUpdate(data);

        return R.ok();
    }


    @ApiOperation(value = "将商品从供应商删除")
    @DeleteMapping(value = "/dropGoods", produces = "application/json; charset=UTF-8")
    public R<Object> dropGoods(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "供应商-商品的id") @RequestParam(value = "id") Long id
    ) {
        GoodsSuppliers data = goodsSuppliersService.getById(id);
        if (data == null) return R.fail("供应商商品获取失败");

        long cnt = whPurchaseGoodsService.getBySupplierIdAndGoodsTypeId(data.getSupplierId(), data.getGoodsTypeId());
        if (cnt > 0) return R.fail("已有该供应商商品关联的采购信息，不能删除");

        long l = goodsSuppliersService.dropData(id);
        if (l <= 0) return R.fail("删除失败");
        return R.ok();
    }


    @ApiOperation(value = "修改商品分类启用状态")
    @PostMapping(value = "/updateEnabled", produces = "application/json; charset=UTF-8")
    public R<Object> updateTypeEnabled(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "id") @RequestParam(value = "id") Long id,
            @ApiParam(value = "启用状态 0.禁用 1.启用") @RequestParam(value = "enabled") Integer enabled
    ) {
        this.goodsSuppliersService.updateEnabled(id, enabled);
        return R.ok();
    }
}